export interface UserAgent {
  ua: string;
  device: {
    type: 'desktop' | 'mobile';
  };
  browser: {
    name: string;
  };
  os: {
    name: string;
  };
}

export const parse = (userAgent: string) => {
  const result: UserAgent = {
    ua: userAgent,
    device: {
      type: 'desktop',
    },
    browser: {
      name: '',
    },
    os: {
      name: '',
    },
  };

  if (/iphone|ipad|ipod|android/i.test(userAgent)) {
    result.device.type = 'mobile';

    // 识别 Browser
    if (/MicroMessenger\//i.test(userAgent)) {
      result.browser.name = 'WeChat';
    }
    else if (/qq\//i.test(userAgent)) {
      result.browser.name = 'QQ';
    }

    // 识别 OS
    if (/android/i.test(userAgent)) {
      result.os.name = 'Android';
    }
    else if (/iphone|ipad|ipod/i.test(userAgent)) {
      result.os.name = 'iOS';
    }
  }

  return result;
}
